﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Utility;
using System.Collections.Generic;
using Telerik.WebControls;

namespace Runda.CMS.Portal.ContractFollowUp
{
    public partial class InstrumentAssemblyExtra : BasePage
    {     
        public Guid MaintainID
        {
            get
            {
                if (hiddenRegisterID.Value == string.Empty) return Guid.Empty;
                else return new Guid(hiddenRegisterID.Value);
            }
            set
            {
                hiddenRegisterID.Value = value.ToString();
            }
        }

        public Guid AssemblyID
        {
            get
            {
                if (hiddenAssemblyID.Value == string.Empty) return Guid.Empty;
                else return new Guid(hiddenAssemblyID.Value);
            }
            set
            {
                hiddenAssemblyID.Value = value.ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {    
                GuaranteeInfo.BindEnum(EnumeratorHelper.GetFieldTexts(typeof(Enumerator.GuaranteeType)));
                ServiceContract.BindEnum(EnumeratorHelper.GetFieldTexts(typeof(Enumerator.ContractServiceType)));
                InstrumentStatus.BindEnum(EnumeratorHelper.GetFieldTexts(typeof(Enumerator.InstrumentStatus)));
                
                            
                if (Request.QueryString["MaintainID"] != null)
                {
                    MaintainID = new Guid(Request.QueryString["MaintainID"]);
                    WsmMaintainBillEntity entity = new SystemBll().Get<WsmMaintainBillEntity>(MaintainID);

                    lnkbtnSave.Visible = false;
                    lnkbtnApprove.Visible = false;
                    lnkbtnSubmit.Visible = false;
                    if (entity.AccessoryCheckStatus == 0 && base.CheckUserHasFuction(1, "Func5111"))
                    { lnkbtnSave.Visible = true; }

                    if (entity.AccessoryCheckStatus >= (int)Enumerator.InstrumentAssmblyStatus.Submit && base.CheckUserHasFuction(1, "Func5112"))
                    { lnkbtnSubmit.Visible = true; }

                    if (base.CheckUserHasFuction(1, "Func5113"))
                    { lnkbtnApprove.Visible = true; }

                    if (base.CheckUserHasFuction(1, "Func5113") && base.CheckUserHasFuction(1, "Func5112"))
                    { lnkbtnSubmit.Visible = false; }

                    //setData1LockStatus(false);
                    //setData2LockStatus(false);
                    //setData3LockStatus(false);

                    //if (base.CheckUserHasFuction(1, "Func5111"))
                    //{ setData1LockStatus(true); }
                    //if (base.CheckUserHasFuction(1, "Func5112"))
                    //{ setData2LockStatus(true); }
                    //if (base.CheckUserHasFuction(1, "Func5113"))
                    //{ setData3LockStatus(true); }

                    
                    WsmTaskRegisterEntity taskEntity = new SystemBll().Get<WsmTaskRegisterEntity>(entity.RegisterID);

                    hidCustomerGUID.Value = taskEntity.CustomerID.ToString();

                    CtrtCustomerEntity customerEntity = new SystemBll().Get<CtrtCustomerEntity>(taskEntity.CustomerID);
                    txtCustomerAddr.Text = customerEntity.CustomerAddress;
                    //客户区域
                    WsmCustomerExtensionEntity CusExtEntity = new SystemBll().Get<WsmCustomerExtensionEntity>(taskEntity.CustomerID);
                    if (CusExtEntity != null)
                    {
                        ddlCustomerArea.SelectedValue = CusExtEntity.CustomerArea;
                    }
                   
                    ManufacInstrumentCode.Text = new SystemBll().Get<WsmInstrumentInfoEntity>(taskEntity.InstrumentID).instrumentCode;
                    List<WsmInstrumentAssemblyEntity> assemblylist = new SystemBll().GetAll<WsmInstrumentAssemblyEntity>(" and MaintainID='" + MaintainID.ToString() + "'");
                    if (assemblylist != null && assemblylist.Count > 0) {
                        WsmInstrumentAssemblyEntity assemblyEntity = assemblylist[0];

                        if (assemblyEntity.SerialNum != null && assemblyEntity.SerialNum.ToString()!="")
                        {
                            SerialNum.Enabled = false;
                        }
                        
                        AssemblyID = assemblyEntity.AssemblyID;
                        InstallDate.SelectedDate = assemblyEntity.InstallDate;
                        GuaranteeInfo.SelectedValue = assemblyEntity.GuaranteeInfo;
                        WarrantyFrom.SelectedDate = assemblyEntity.WarrantyFrom;
                        WarrantyTo.SelectedDate = assemblyEntity.WarrantyTo;
                        GuaranteeFrequency.Value = System.Convert.ToDouble(assemblyEntity.GuaranteeFrequency);
                        ManufacInstrumentCode.Text = assemblyEntity.ManufacInstrumentCode;
                        ServiceContract.SelectedValue = assemblyEntity.ServiceContract;
                        CompanyInstrumentCode.Text = assemblyEntity.CompanyInstrumentCode;
                        ReturnVisitFrequency.Value = System.Convert.ToDouble(assemblyEntity.ReturnVisitFrequency);
                        SoftWareVersion.Text = assemblyEntity.SoftWareVersion;
                        HasTrainCertificate.SelectedValue = assemblyEntity.HasTrainCertificate.ToString();
                        CalibrationFrequency.Value = System.Convert.ToDouble(assemblyEntity.CalibrationFrequency);
                        SerialNum.Text = assemblyEntity.SerialNum;
                        InstrumentBeginUse.SelectedDate = assemblyEntity.InstrumentBeginUse;
                        InstrumentBelongto.Text = assemblyEntity.InstrumentBelongto;
                        InstrumentMode.Text = assemblyEntity.InstrumentMode;
                        InstrumentApplicateSituation.Text = assemblyEntity.InstrumentApplicateSituation;
                        InstrumentStatus.SelectedValue = assemblyEntity.InstrumentStatus;
                        HasReadNum.SelectedValue = assemblyEntity.HasReadNum.ToString();
                        DayEstimate.Value = System.Convert.ToDouble(assemblyEntity.DayEstimate);
                        ReadNumType.SelectedValue = assemblyEntity.ReadNumType.ToString();
                        GuaranteebeginDate.SelectedDate = assemblyEntity.GuaranteebeginDate;
                        ReturnVisitbeginDate.SelectedDate = assemblyEntity.ReturnVisitbeginDate;
                        CalibrationbeginDate.SelectedDate = assemblyEntity.CalibrationbeginDate;
                        ConservationManGUID.Value = assemblyEntity.ConservationMan.ToString();
                        txtdy1.Text = assemblyEntity.dy1;
                        txtdy2.Text = assemblyEntity.dy2;
                        txtInstrumentWhere.Text = assemblyEntity.InstrumentWhere;
                        txtWhereTel.Text = assemblyEntity.WhereTel;
                        txtSales.Text = assemblyEntity.Sales;
                        txtAgencyName.Text = assemblyEntity.AgencyName;
                        txtAgencyMan.Text = assemblyEntity.AgencyMan;
                        txtAgencyTel.Text = assemblyEntity.AgencyTel;
                        txtMemo.Text = assemblyEntity.Memo;

                        if (assemblyEntity.HasAgency != null)
                        { ddlHasAgency.SelectedValue = assemblyEntity.HasAgency; }

                        if (ConservationManGUID.Value != "00000000-0000-0000-0000-000000000000")
                        {
                            ConservationManName.Text = new SystemBll().Get<SysUserEntity>(new Guid(ConservationManGUID.Value)).UserName;
                        }
                        AdjustManGUID.Value = assemblyEntity.AdjustMan.ToString();
                        if (AdjustManGUID.Value != "00000000-0000-0000-0000-000000000000")
                        {
                            AdjustManName.Text = new SystemBll().Get<SysUserEntity>(new Guid(AdjustManGUID.Value)).UserName;
                        }
                        returnVisitManGUID.Value = assemblyEntity.ReturnVisitMan.ToString();
                        if (returnVisitManGUID.Value != "00000000-0000-0000-0000-000000000000")
                        {
                            returnVisitManName.Text = new SystemBll().Get<SysUserEntity>(new Guid(returnVisitManGUID.Value)).UserName;
                        }
                        ReadNumManageManGUID.Value = assemblyEntity.ReadNumManageMan.ToString();
                        if (assemblyEntity.ReadNumManageMan != System.Guid.Empty)
                        {
                            ReadNumManageManName.Text = new SystemBll().Get<SysUserEntity>(new Guid(ReadNumManageManGUID.Value)).UserName;
                        }
                        ReadNumBeginDate.SelectedDate = assemblyEntity.ReadNumBeginDate;
                        ReadNumFrequency.Value = System.Convert.ToDouble(assemblyEntity.ReadNumFrequency);


                    }
                }            
            }
        }

        protected void lnkbtnSave_Click(object sender, EventArgs e)
        {
            string Message = "";
            if (!save(ref Message))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "saveerror", "alert('" + Message + "');", true);
            }
            else
                ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功')", true);

            //MaintainBillBll bll = new MaintainBillBll();
            //bll.UpdateMaintainBillCheckStatus(MaintainID, Enumerator.InstrumentAssmblyStatus.Submit, Guid.Empty, "");
        }
        
        private bool save(ref string message) {
            bool isSuccess = true;
            WsmMaintainBillEntity maintainEntity = new SystemBll().Get<WsmMaintainBillEntity>(MaintainID);
            WsmInstrumentAssemblyEntity entity = new WsmInstrumentAssemblyEntity();
            maintainEntity.InstrumentSerialNum = SerialNum.Text.Trim();
            SystemBll bll = new SystemBll();
            if (AssemblyID != Guid.Empty)
            {
                entity = bll.Get<WsmInstrumentAssemblyEntity>(AssemblyID);
                if (entity == null)
                {
                    entity = new WsmInstrumentAssemblyEntity();
                    AssemblyID = entity.AssemblyID;
                    entity.MaintainID = MaintainID;
                    entity.Deleted = false;
                }
            }
            else
            {
                
                entity = new WsmInstrumentAssemblyEntity();
                AssemblyID = entity.AssemblyID;
                entity.MaintainID = MaintainID;
                entity.Deleted = false;
            }
            if (new MaintainBillBll().isExistSerialNum(entity.AssemblyID.ToString(),SerialNum.Text.Trim()) > 0)
            {
                message = "输入的序列号已经存在，请重新输入！";
                isSuccess = false;
                return isSuccess;
            }
            //if (returnVisitManGUID.Value == "") {
            //    message = "请输入回访人！";
            //    isSuccess = false;
            //    return isSuccess;
            //}
            //if (AdjustManGUID.Value=="")
            //{
            //    message = "请输入校准人！";
            //    isSuccess = false;
            //    return isSuccess;
            //}
            //if (ConservationManGUID.Value =="")
            //{
            //    message = "请输入保养人！";
            //    isSuccess = false;
            //    return isSuccess;
            //}
            if(InstallDate.SelectedDate != null)
                entity.InstallDate =InstallDate.SelectedDate.Value;
            entity.GuaranteeInfo=GuaranteeInfo.SelectedValue.Trim();
            if(WarrantyFrom.SelectedDate != null)
                entity.WarrantyFrom = WarrantyFrom.SelectedDate.Value;
            if (WarrantyTo.SelectedDate != null)
                entity.WarrantyTo =  WarrantyTo.SelectedDate.Value;
            entity.GuaranteeFrequency = System.Convert.ToInt32(GuaranteeFrequency.Value);
            entity.ManufacInstrumentCode = ManufacInstrumentCode.Text.Trim();
            entity.ServiceContract = ServiceContract.SelectedValue;
            entity.CompanyInstrumentCode =CompanyInstrumentCode.Text.Trim();
            entity.ReturnVisitFrequency = System.Convert.ToInt32(ReturnVisitFrequency.Value);
            entity.SoftWareVersion = SoftWareVersion.Text.Trim();
            entity.HasTrainCertificate = System.Convert.ToInt32(HasTrainCertificate.SelectedValue);
            entity.CalibrationFrequency = System.Convert.ToInt32(CalibrationFrequency.Value) ;
            entity.SerialNum = SerialNum.Text.Trim();
            if(InstrumentBeginUse.SelectedDate != null)
                entity.InstrumentBeginUse = InstrumentBeginUse.SelectedDate.Value;
            entity.InstrumentBelongto =  InstrumentBelongto.Text.Trim();
            entity.InstrumentMode = InstrumentMode.Text.Trim();
            entity.InstrumentApplicateSituation = InstrumentApplicateSituation.Text;
            entity.InstrumentStatus = InstrumentStatus.SelectedValue;
            entity.HasReadNum = System.Convert.ToInt32(HasReadNum.SelectedValue);
            entity.DayEstimate = System.Convert.ToDecimal(DayEstimate.Value);
            entity.ReadNumType = System.Convert.ToInt32(ReadNumType.SelectedValue);
            if (GuaranteebeginDate.SelectedDate != null)
                entity.GuaranteebeginDate = GuaranteebeginDate.SelectedDate.Value;
            if (ReturnVisitbeginDate.SelectedDate != null)
            entity.ReturnVisitbeginDate = ReturnVisitbeginDate.SelectedDate.Value;
            if (ConservationManGUID.Value != "")
            {
                entity.ConservationMan = new Guid(ConservationManGUID.Value);
            }
            if (AdjustManGUID.Value != "")
            {
                entity.AdjustMan = new Guid(AdjustManGUID.Value);
            }
            if (returnVisitManGUID.Value != "")
            {
                entity.ReturnVisitMan = new Guid(returnVisitManGUID.Value);
            }
            entity.dy1 = txtdy1.Text;
            entity.dy2 = txtdy2.Text;
            entity.InstrumentWhere = txtInstrumentWhere.Text;
            entity.WhereTel = txtWhereTel.Text;
            entity.Sales = txtSales.Text;
            entity.AgencyName = txtAgencyName.Text;
            entity.AgencyMan = txtAgencyMan.Text;
            entity.AgencyTel = txtAgencyTel.Text;
            entity.Memo = txtMemo.Text;

            if (ddlHasAgency.SelectedValue != "9")
            {
                entity.HasAgency = ddlHasAgency.SelectedValue.ToString();
            }
            else
            { entity.HasAgency = null; }
            if (CalibrationbeginDate.SelectedDate != null)
            entity.CalibrationbeginDate = CalibrationbeginDate.SelectedDate.Value;
            if (ReadNumManageManGUID.Value != "")
            {
                entity.ReadNumManageMan = new Guid(ReadNumManageManGUID.Value);
            }

            if (ReadNumBeginDate.SelectedDate != null)
            {
                entity.ReadNumBeginDate = ReadNumBeginDate.SelectedDate.Value;
            }
            entity.ReadNumFrequency = System.Convert.ToInt32(ReadNumFrequency.Value);

            bll.InsertOrUpdate(entity);
            //更新客户地址
            CtrtCustomerEntity customerEntity = new SystemBll().Get<CtrtCustomerEntity>(new Guid(hidCustomerGUID.Value));
            customerEntity.CustomerAddress = txtCustomerAddr.Text;
            bll.InsertOrUpdate(customerEntity);
            WsmCustomerExtensionEntity customerEntityex = new SystemBll().Get<WsmCustomerExtensionEntity>(new Guid(hidCustomerGUID.Value));
            customerEntityex.CustomerArea = ddlCustomerArea.SelectedValue;
            bll.InsertOrUpdate(customerEntityex);

            //将序列号更新到 派工单 工作单 读数
            bll.Update(maintainEntity);
            WsmTaskRegisterEntity taskEntity = bll.Get<WsmTaskRegisterEntity>(maintainEntity.RegisterID);
            taskEntity.InstrumentSerialNum = SerialNum.Text.Trim();
            bll.Update(taskEntity);

            List<WsmInstrumentRecordEntity> recordList = bll.GetAll<WsmInstrumentRecordEntity>(" and MaintainID='" + MaintainID.ToString() + "'");
            foreach(WsmInstrumentRecordEntity recordEntity in recordList){
                recordEntity.SerialNum = SerialNum.Text.Trim();
                bll.Update(recordEntity);
            }
            return isSuccess;
        }

        protected void lnkbtnSubmit_Click(object sender, EventArgs e)
        {

            string Message = "";
            if (!save(ref Message))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "saveerror", "alert('" + Message + "');", true);
                return;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功')", true);

                MaintainID = new Guid(Request.QueryString["MaintainID"]);
                WsmMaintainBillEntity entity = new SystemBll().Get<WsmMaintainBillEntity>(MaintainID);
                
                if (entity.AccessoryCheckStatus != 3)
                {
                    MaintainBillBll bll = new MaintainBillBll();
                    bll.UpdateMaintainBillCheckStatus(MaintainID, Enumerator.InstrumentAssmblyStatus.Checked, Guid.Empty, "");
                }
                lnkbtnSubmit.Enabled = false;
                lnkbtnSave.Enabled = true;
            }

        }

        private void setData1LockStatus(Boolean lockStatus)
        {
            InstallDate.Enabled = lockStatus;
            InstrumentBeginUse.Enabled = lockStatus;
            SoftWareVersion.Enabled = lockStatus;
            txtdy1.Enabled = lockStatus;
            txtdy2.Enabled = lockStatus;
            HasReadNum.Enabled = lockStatus;
            ReadNumType.Enabled = lockStatus;
            txtCustomerAddr.Enabled = lockStatus;
            ddlCustomerArea.Enabled = lockStatus;
            txtInstrumentWhere.Enabled = lockStatus;
            txtWhereTel.Enabled = lockStatus;
        }

        private void setData2LockStatus(Boolean lockStatus)
        {
            GuaranteeFrequency.Enabled = lockStatus;
            GuaranteebeginDate.Enabled = lockStatus;
            ReturnVisitFrequency.Enabled = lockStatus;
            ReturnVisitbeginDate.Enabled = lockStatus;
            CalibrationFrequency.Enabled = lockStatus;
            CalibrationbeginDate.Enabled = lockStatus;
            ReadNumFrequency.Enabled = lockStatus;
            ReadNumBeginDate.Enabled = lockStatus;
        }

        private void setData3LockStatus(Boolean lockStatus)
        {
            GuaranteeInfo.Enabled = lockStatus;
            WarrantyFrom.Enabled = lockStatus;
            WarrantyTo.Enabled = lockStatus;
            DayEstimate.Enabled = lockStatus;
            HasTrainCertificate.Enabled = lockStatus;
            txtSales.Enabled = lockStatus;
            ddlHasAgency.Enabled = lockStatus;
            txtAgencyName.Enabled = lockStatus;
            txtAgencyMan.Enabled = lockStatus;
            txtAgencyTel.Enabled = lockStatus;
        }

        protected void lnkbtnApprove_Click(object sender, EventArgs e)
        {
            string Message = "";
            if (!save(ref Message))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "saveerror", "alert('" + Message + "');", true);
                return;
            }
            else
            {
                MaintainBillBll bll = new MaintainBillBll();
                bll.UpdateMaintainBillCheckStatus(MaintainID, Enumerator.InstrumentAssmblyStatus.Approved, CurrentUserID, CurrentUserEntity.UserName);

            }

        }
    }
}
